package com.itheima.search.controller;

import com.itheima.article.pojo.ApArticle;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.pojo.Result;
import com.itheima.search.dao.ArticleDao;
import com.itheima.search.document.ArticleInfoDocument;
import com.itheima.search.dto.SearchDto;
import com.itheima.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author：shenkunlin
 * @date： 2021/9/2 11:54
 * @Description：
 ***/
@RestController
public class SearchController {

    @Autowired
    private SearchService searchService;

    /****
     * 搜索实现
     * 查询->关键词搜索
     *   A:搜索->封装搜索条件对象XxxQuery,封装搜索条件
     *   B:配置高亮域->指定高亮域、指定高亮的前缀、后缀
     *   C:分页搜索
     *   D:解析结果集
     *        1:非高亮数据
     *        2:高亮数据
     *        3:将非高亮数据替换成高亮数据
     */
    @PostMapping(value = "/search")
    public Result<PageInfo<ArticleInfoDocument>> search(@RequestBody SearchDto dto){
        PageInfo<ArticleInfoDocument> pageInfo = searchService.search(dto);
        return Result.ok(pageInfo);
    }

    /**
     * 增加单个文章到ES索引库中
     */
    @PostMapping(value = "/saveToEs")
    public Result saveToEs(@RequestBody ArticleInfoDocument document){
        searchService.saveToEs(document);
        return Result.ok();
    }
}
